package com.jfinal.weixin.sdk.api;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.utils.HttpUtils;

import cn.com.longfuinfo.wx.kit.DoubleKit;
import cn.com.longfuinfo.wx.util.PayUtil;
import cn.com.longfuinfo.wx.util.XmlUtil;

public class PayMkttransfers {
	private static final String TRANSFERS_PAY = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; // 企业付款
	private static final String APP_ID = PropKit.get("wx.appId");
	private static final String MCH_ID = PropKit.get("wx.mchid");
	private static final String API_SECRET = PropKit.get("wx.api.appSecret");

	public static Map<String, String> paymkttransfers(String openid,String point){
		Map<String, String> restmap = null;
		try {
		Map<String, String> parm = new HashMap<String,String>();
		parm.put("mch_appid", APP_ID); //公众账号appid
		parm.put("mchid", MCH_ID); //商户号
		parm.put("nonce_str", PayUtil.getNonceStr()); //随机字符串
		parm.put("partner_trade_no", PayUtil.getTransferNo()); //商户订单号
		parm.put("openid", openid); //用户openid	
		parm.put("check_name", "NO_CHECK"); //校验用户姓名选项 OPTION_CHECK
		//parm.put("re_user_name", "安迪"); //check_name设置为FORCE_CHECK或OPTION_CHECK，则必填
		String point2=String.valueOf((int)(DoubleKit.mul(Double.parseDouble(point),100)));
		parm.put("amount", point2); //转账金额
		parm.put("desc", "积分提现"); //企业付款描述信息
		String addr = InetAddress.getLocalHost().getHostAddress();//获得本机IP
		parm.put("spbill_create_ip", addr); //Ip地址
		
		parm.put("sign", PayUtil.getSign(parm, API_SECRET));
		String certPath = PathKit.getRootClassPath()+"/"+PropKit.get("wx.cert");
		String restxml = HttpUtils.postSSL(TRANSFERS_PAY, XmlUtil.xmlFormat(parm, false),certPath,MCH_ID);
		restmap = XmlUtil.xmlParse(restxml);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return restmap;
	}
}
